package com.xiaoxi.bms.service.log.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxi.bms.domain.log.BMSLogOperation;
import com.xiaoxi.bms.domain.log.dto.BMSLogOperationDTO;
import com.xiaoxi.bms.domain.log.vo.BMSLogOperationVO;
import com.xiaoxi.bms.mapper.log.BmsLogOperationMapper;
import com.xiaoxi.bms.service.common.circulation.ExportDataToExcelService;
import com.xiaoxi.bms.service.log.BmsLogOperationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName BmsLogOperationServiceImpl
 * @Description
 * @Author xiaoxi
 * @Date 2022-10-31
 * @Version 1.0
 */
@Service("bmsLogOperationServiceImpl")
@Slf4j
public class BmsLogOperationServiceImpl extends ServiceImpl<BmsLogOperationMapper, BMSLogOperation> implements BmsLogOperationService, ExportDataToExcelService {

    @Override
    public IPage<BMSLogOperationVO> findListByPage(BMSLogOperationDTO bmsLogOperationDTO, Pageable pageable) {

        IPage<BMSLogOperation> iPage = new Page<>(pageable.getPageNumber(), pageable.getPageSize());

        QueryWrapper<BMSLogOperation> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(bmsLogOperationDTO.getUsername())) {
            queryWrapper.like("username", bmsLogOperationDTO.getUsername());
        }
        if (Objects.nonNull(bmsLogOperationDTO.getBeginTime()) && Objects.nonNull(bmsLogOperationDTO.getEndTime())) {
            queryWrapper.between("operation_time", bmsLogOperationDTO.getBeginTime(), bmsLogOperationDTO.getEndTime());
        }
        iPage = baseMapper.selectPage(iPage, queryWrapper);

        List<BMSLogOperationVO> bmsLogOperationVOList = new ArrayList<>();
        iPage.getRecords().forEach(BMSLogOperation -> {
            BMSLogOperationVO bmsLogOperationVO = new BMSLogOperationVO();
            BeanUtils.copyProperties(BMSLogOperation, bmsLogOperationVO);
            bmsLogOperationVOList.add(bmsLogOperationVO);
        });
        IPage<BMSLogOperationVO> bmsLogOperationVOIPage = new Page<>(pageable.getPageNumber(), pageable.getPageSize());
        bmsLogOperationVOIPage.setRecords(bmsLogOperationVOList);
        bmsLogOperationVOIPage.setTotal(iPage.getTotal());
        return bmsLogOperationVOIPage;
    }

    @Override
    public void add(BMSLogOperation bmsLogOperation) {
        baseMapper.insert(bmsLogOperation);
    }

    @Override
    public void delete(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void update(BMSLogOperation bmsLogOperation) {
        baseMapper.updateById(bmsLogOperation);
    }

    @Override
    public BMSLogOperation findById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<String> tableHeader(String... args) {

        List<String> tableHeader = new ArrayList<>();

        tableHeader.add("序号");
        tableHeader.add("用户名");
        tableHeader.add("访问ip地址");
        tableHeader.add("请求uri");
        tableHeader.add("请求方法");
        tableHeader.add("请求参数(需要进行UrlDecode解码)");
        tableHeader.add("响应体(需要进行UrlDecode解码)");
        tableHeader.add("操作日志(需要进行UrlDecode解码)");
        tableHeader.add("操作码");
        tableHeader.add("操作状态");
        tableHeader.add("操作时间");
        tableHeader.add("操作动作");
        tableHeader.add("操作描述");

        return tableHeader;

    }

    @Override
    public <T> IPage<T> tableContent(T objDTO, Pageable pageable, String... args) {
        return (IPage<T>) findListByPage((BMSLogOperationDTO) objDTO, pageable);
    }
}
